refactored code for clarity