implemented speedup, runs in 3.7 sec