improved invalid tag error message; some code cleaning