Improved logic for tags search