restructure a function reducing nesting depth.