Added more debug messages to code to make it easier to catch errors.