more robust error handling, should fix #3