Major refactoring, steal some prefs from berger and breno. Thanks