Use setImmediate instead of process.nextTick to suppress nodejs deprecation warnings

Current code causes the warning: 
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.