Fix a number of issues.

Handle the new document well. Count samples better. Better parsing.