Major overhaul on database querying