many, many bug fixes, particularly for error cases