Added more checks to the callback function