restructured code to be less one-huge-fucking-file-ish