Refactor parts of the code