Getting gibberish when writing to an array