major changes (about 10-20% speedup)