Use setImmediate to avoid nextTick stack overflow