Several bug fixes and added logging.