Added Debug mode variables and did some refactoring