I may have just found the bug - silly mistake because I forgot that arrays are zero based.