( edit: I added the best working approach in my augmented reality framework and now also take the gyroscope into account which makes it much more stable again: Droid
Note that if you are getting consistently wrong readings, you may have to calibrate your compass, by moving it with your wrists in a figure 8.
Hard to explain this in words; watch this video: http://www.youtube.com/watch?v=sP3d00Hr14o