re-structure nested if/else logic and reformat per code convention; move block level declaration to top of function to avoid confusion around scoping level