fixed some edge cases which caused crashes during startup and shutdown