A few fixes.

This is basically a re-submission of my last pull request, #33, this one cleaned of spurious coding experiments.
The messages for the single commits explain what is happening.