Working on revamping algorithm