debugging Unity crash changes, some fixes to algorithm