refactor data loading code