added logging and refactored the global variables