corrected logic flaws, built test program

Made some changes to fix issues with caching algorithm