Fixed a bug where exclusive ranges were being generated incorrectly

Added in a following key function that implements similar functionality to the Java implementation. Also updated relevant unit tests.