change nextTick usage to setImmediate