Cleaning up of code and added some debugging helper code