added tests and changed implementation details