[fix][tidy] added memory management

improved logs
improved logic flow