refactored some db logic from view to model