Some refactoring and more unit tests