cleanup procedures in main, more work on error handler