Fix missing includes for Cocoa and move some functions into and out of
main into the files they belong in.