Moved helper functions around a little