improve logic of the callback function