Refactored some helper functions