Fix shutdown to correctly close stdout and stop showing garbage characters on exit.
Patch by Matthew Sorrels