first draft of solution
rewrite the main parsing loop to use named captures for clarity
more comments for clarity