Added log

Note:
- Added debug logs and fixed code for defensive programming... -_-;;;