Lots of logic fixes.