Fixed some bugs and formatted code