Unsafe operations in play as I realised there was a bug with the old one.

Will fix later.
Use a bit more elegant IORef instead of manually forcing writes with unsafePerformIO.