Add braces everywhere and fix indentation

Plus some loop merges