Some more code cleaning and refactoring