code refactored and added model, business logic