Lots of logic fixes to alga