Adding toArray function implementation