Refactoring and removing dead code