added failing test based on issue spotted by @jrrideout
fixed algorithmic issue spotted by @jrrideout