Images and db refactor for queries