Bug fixes and added tests