moved createpreview code in its own file