Refactor and add shutdown method