Fixed bugs by adding error handlers. This needs more work to be airtight.