An improved algorithm for micromagnetics