Changed list to dynamic memory and delete memory when removed from list