Added freeing of old array in addElement function