Refactor towards to using the state monad

At least, I /think/ this is the correct direction