refactored loading code