improved setImmediate function