added ability to cache already visited states