rewritten preload function, fixed bug