Added some logging and debugging  functionality