Using setImmediate to allow all sync code to finish before doing the callback