added some logging and refactored global variables