Moved helper functions to the top