added tests, fixed bugs