Bounds checking

Added boundary checking for the array to prevent a security exploit.