Added many tests, fixed bugs