fixed lots of problems with the algo