added caching; rearranged query call