Refactor stuff out of IO monad.