Improve regex for tag parsing: allow more sloppy tag format. Move tag parsing into separate function.