Some more refactoring and comments