Working basic algorithm.