Bugfixes, performance improvements, more tests, and some cleanup