Wrote out a better algorythm