did a lot of cleanup and exception handling