Refactor utility functions