fix a bunch of general bugs causing crashes and add proper to-file logging.