Refactor and add delete function