Fully working algorithm. ~10 second runtime for ks_100_0