Huh, when I go into your debug tab the heading value always seems to stay accurate no matter if my phone is flat or vertical. It seems to work the same as the Compass app, which similarly stays accurate.
However, I do see that the drawn arrow totally breaks down when the phone is held vertically. So I wonder if maybe it's a bug somewhere in the calculations or drawing?