Reformatted and refactored code for readability