Refactored some functions