Error: reference index always being set to zero