Minor refactoring for legibility. Added logging.