All spec tests pass.
Added spec tests to catch some corner cases, as well as fixes for them.