Lotta bug fixes

And regressions fixed, and tests written