[Edit] Refactored code