fixed bugs and added tests