Added tests and minor refactorings