added tests and bug fixes