Move internal functions to top so promises are returned