Refactor HTTP server code