Set up persistent storage for settings

Added settings and set up persistent storage of the values in anticipation of setting up a configuration option.

Also refactored some of the code to make it nicer and work better.