Some refactoring and cleaning