Linux fixes

Major crashes at startup and shutdown seem to be fixed now, and the changes are ready for testing on Windows.