Bug fixes and testes