original algorithm - pseudocode