Refactor load function to reduce complexity