Some more refactoring and started unit testing