Refactor and tidying