Remove a lot of unnecessary mutable variables to find the curly braces. Instead, use a bit of functional programming to achieve the algorithm.