replacing setTimeout(fn,0) by setImmediate