fixed bugs + added tests