Refactored and removed unnecessary code
Added support function