more refactoring, but need to stop nesting callback functions