wrap preview code in a function