moving logic from controllers here for common use