Updated toArray function to return objects