Finish writing basic algorithm