refactored code for readability, reduced complexity