fix function scoping issues