Added some more missing braces and some minor refactoring