fix the code so not to invoke callback twice