Some code cleanup and attempts to prevent service from shutting down.