How can I correctly decode CPR positions from ADS-B frames? I am decoding Compact Position Reporting (CPR) positions from ADS-B frames (the airborne position and surface position frames).

Most of my results give realistic longitude/latitude values, but some return things like 0,781518968485169 177,220642089844; or 1,76377118644068    -0,664306640625; or 2,12129237288136 -2,838134765625.

I have a feeling like I must be missing some kind of validation or some kind of frame type identification. Where can I find more information about the decoding process?
We were decoding Airborne Position frames having TypeCode 0, 9-18 and 20-22.
Apparently, frames with TypeCode 20-22 and possibly 0 have to be decode a different way.

(For now I think that I get enough information out of TypeCode 9-18 frames so I'll just skip the other ones.)