beginning of first algorithm