Fixed some bugs, added logging.