Bug fixes and test suite