Fixes an off-by-one check and improves the error output.