better error handling; minor bug fixes