Refactor and formatting