major cleanup and algorithm changes