refactored and simplified code for presentation