Bug fixes and more tests