Refactor and delete some code