Refactor and remove unused codes