Refactored code (minimized)