refactor API code, make http errors render correctly