Fixed a casting bug and added some more logging.