wrote the basic algorithm