added tests and fixed bugs everywhere