public class ActionScript
{
    private ContextProvider Context = ScriptManager.Context;

    public bool Execute(List<IMinerBase> list)
    {
        Context.System.LogMessage("Pool Balance Script: " + list.Count + " miner(s).");

        // Process each selected miner
        foreach (IMinerBase miner in list)
        {
            if (!miner.IsRunningState)
                continue;

            Context.System.LogMessage("Pool Balance Script processing: " + miner.GetDescription());
            ISinglePool currentPool = Context.Pool.GetActive(miner);

            if (currentPool != null)
            {
                ISinglePool lowestPrioPool = null;
                int lowestPrio = 0;

                // Get all active pools for the miner
                List<ISinglePool> activePoolList = Context.Pool.GetActivePoolList(miner, false, true, miner.IsPrimitive);

                // Find pool with lowest priority (highest number returned by GetPriority)
                foreach (ISinglePool activePool in activePoolList)
                {
                    IPoolStatus poolStatus = miner.GetPoolStatus(activePool);
                    if (poolStatus != null)
                    {
                        int prio = poolStatus.GetPriority();
                        if (prio > lowestPrio)
                        {
                            lowestPrio = prio;
                            lowestPrioPool = activePool;
                        }
                    }
                    else
                        Context.System.LogMessage("  No pool status available: " + activePool.GetDescription());
                }

                // Change pool
                if (lowestPrioPool != null)
                {
                    Context.System.LogMessage("Pool Balance Script prioritize: " + lowestPrioPool.GetDescription());
                    Context.Pool.Prioritize(miner, lowestPrioPool);
                }

            }
        }
        return true;
    }
}