solved some error and added Dharak's Code
improved toArray