Refactor some bits of code