UPDATE: Refactored code