Code refactoring and simplification