Bug fixes, WIP: algorithm