Code cleanup: sparse array handling, etc.

Fixes #302.