Various performance improvements

Through three changes, a total of about 30% performance improvement (from 170s to 120s).