Added tests, fixed bugs