replaced setTimeout with setImmediate plus reliable osx implementation