Refactored code. Added func