Fixes #10: Simplify code and use SparseArray