Refactored the code for simplicity.