added null check
toArray -> toArrayCopy (and is now public)