Refactor some code