Fixed a few bugs and formatted code