Fix wrong boundaries check causing segfault