Fixing off-by-one bug