Fixed dealing with file output.

Forget to add back functionality. Therefore I separated the functions
out and cleaned up the code a bit.