Adding unit tests + refactoring