Tests and fixes for issue #94

Addresses the lack of tests in issue #94 and fixes a bug with a possible unreferenced variable (in `flags`)

The commit from @thepeopleseason has been kept.