Bounds checking to avoid segfault