refactored for better variable names and more separation of concerns