fixed some bug prone code, adding logs