Make error handling more robust in the server.

Fix #23.