Added a hash set for visited states. Serious speed-up.