Move every boundary check to max_boundary check to avoid unnecessary checks