Refactored address count query