added tests and some minor refactorings