Bunch of fixes and code formatting