Refactor mysql data pull code