Fix crash bug and format code