Simplify algorithm a bit -- still not working