functioning code. about 65% accuracy