Code refactoring and duplicate code removal