CGI code cleanup (type signatures, unused code, sensible code ordering)