Fix invalid memory access

Fixes crash in #1, plus other two cosmetic (but nice to have) commits.