Refactored the code to be more readable. No functional changes.