added preview and refactored some code