Move lowering logic out of main.
Begin refactoring towards global list of functions.