moved helper functions to bottom