added debug logs and some minor logic