Bug fix in toArray method