Fixed some race conditions, added more logging.