Using setImmediate when necessary