Refactor helper functions