Fix formatting and refactor copy-paste duplication