added error messages, and moved at_exit