lots of cleanup here, all unit tests are localized, core algorithm cleaned up a bit