Refactored code and added more functions