Refactor and format codes