Move db queries out of routing script, implement connection pooling