Light refactoring and adding more unit tests