Part one of algorithm