Added minimal exception handling and some basic cleanup