Refactor memes_query code to package it and clean it