added tests and fixed lots of bugs