improved range query handling