fixed getNextInvalid implementation
added ability to find end of valid range