code cleanup and better source preview behavior