def update_state(reactants, products, state, rxn_ind, reverse):
    """
    Updating the system state based on chosen reaction, during kMC simulation.

    Args:
        reactants: (n_rxns x 2) array, each row containing reactant mol_index of forward reaction
        products: (n_rxns x 2) array, each row containing product mol_index of forward reaction
        state: array of initial molecule amounts, indexed corresponding to reaction_network.entries_list
        rxn_ind: int of reaction index, corresponding to position in reaction_network.reactions list
        reverse: bool of whether this is the reverse reaction or not

    :return: updated state array, after performing the specified reaction
    """

    if rxn_ind == -1:
        raise RuntimeError("Incorrect reaction index when updating state")
    if reverse:
        for reactant_id in products[rxn_ind, :]:
            if reactant_id == -1:
                continue
            else:
                state[reactant_id] -= 1
                if state[reactant_id] < 0:
                    raise ValueError("State invalid! Negative specie encountered")
        for product_id in reactants[rxn_ind, :]:
            if product_id == -1:
                continue
            else:
                state[product_id] += 1
    else:
        for reactant_id in reactants[rxn_ind, :]:
            if reactant_id == -1:
                continue
            else:
                state[reactant_id] -= 1
                if state[reactant_id] < 0:
                    raise ValueError("State invalid! Negative specie encountered")
        for product_id in products[rxn_ind, :]:
            if product_id == -1:
                continue
            else:
                state[product_id] += 1

    return state