refactor and add delete[]