Added logic to detect start and end tags