added some logging and refactored the global variables