basic algorithm finished