Finnished prob1 implementation