Fixed bugs and formatted code