massively refactored, much cleaner state monad

still messy, more cleanup work to do