added tests and fixed bugs