Implementing the custom query logic