Pretty much a rewrite but its giving me useful results