[Edit] Refactored code and added documentations