Refactored. We check that closing brace is of the deepest level and that the number of opening and closing braces is the same.