Rearranged code to make clear what the API is