Added test, fixed bugs