Updated code/algorithm. Will need to fix because slow