Added platform specific code for terminating gracefully.