Add unit tests + refactoring