Refactored logical query

It's cleaner now but I still feel it can be done better