Did some refactoring and code cleaning