Changed chooseLevel to use a TVar instead of unsafePerformIO