Refactoring and improving code