Better error handling

If something goes wrong don't crash.