Added comments to the code and wrote a debug function to enable/disable debugging messages as well log levels