Fixed algo to better satisfy reqs
fixed merge conflicts