Refactor some of the code