refactor out code to run_analysis func