Fixed an exception being thrown when the application exits