Resolve scoping issues
Function renaming and reordering