Fix memory leak when exiting via ctl-d
Clean up code formatting