More improvements to code formatting, variable scope