Major refactor to use pandas