Refactored cgi-handler internals