Removed race condition that could prevent application from being terminated correctly