Fixed bugs. Added logging.