simplify error handling; some cleanup