Added bounds checking to prefix matching