Added some extra bounds checking