Code cleanup

Cleaning up the code a bit
Fix crash on preview restart