cleanup, speedup, algo improvements