Fix issues for  DSC-389

Also reformat some code to make it consistant within the file. (hence all the weird changes).