We should move this into a library, I think it's copy-pasta from Heroku's stuff right?