Format and refactor code