Tests and fixes for issue #24

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