added logging, refactored global variables

Refactored some code with additional logging