Fixed bug in toArray method