WIP: util.jl refactor crashes mac

This branch contains @nolta 's refactoring of util.jl, which was crashing on the mac. I can't really tell why this crashes. 

I have reinstated the patch here, and am hoping that we can collectively figure out a way to fix this. @vtjnash has a stacktrace in #2592.