refactored code with preferences