added non-determinism, refactored a bunch