Code clean up and performance boost on local querying. (v3.7.4)