Refactor out database calls from handler code.