refactored code to consider tags