moved logic from controllers to models