refactoring for legibility and console logging