Add output file (TODO); Centralizing exiting of functions to improve cleanup.