cleaned up code with helper functions