better optimization routine