The working version with a database and  fixing preferences for SDK>19