Fixing array out of bounds exception