Refactor to include helper functions