Refactor and reformat