Added some boundary checking