Added some logging events and fixed the code abit