Some queries are optimized.
Minor fixes.