Fix logic and format code.