Some refactories and query improvements.