Refactore some code