Bug fix
Made energy minimization more robust