Fix bugs and format codes