Improving adding and removing curly brackets
Various minor improvements when removing or adding curly brackets
e.g. around titles:
- correctly checking if add or removal operation is necessary
- moving variable declarations inside for-loops where necessary
- adding/updating/correcting comments