added tests + some refactoring