Refactored for ease of reading.