Improved error handling to prevent service crashing. Fixes #38 and #29.