updating error reporting and cleaning up code