refactoring and adding preload function