Refactor settings code to avoid exit calls