Added shutdown hook to save prefernces.