FreeGp:=FreeGroup("A","B");
Af:=FreeGp.1;
Bf:=FreeGp.2;

Rels:=[
(Bf*Af*Bf*Af^-1)^3,
(Bf*Af*Bf^-3*Af^-1)^3,
(Bf^2*Af*Bf*Af^-1*Bf^-1*Af^2)^3,
Af^-1*Bf^-1*Af^-3*Bf*Af*Bf*Af^-1*Bf^-1*Af*Bf^3*Af*Bf^-3*Af^-2,
Bf^2*Af*Bf^4*Af^-1*Bf*Af*Bf*Af^-1*Bf^-1*Af^2*Bf*Af^3*Bf^3*Af*Bf,
Bf^-1*Af^2*Bf^-3*Af^-3*Bf*Af*Bf^-1*Af^-1*Bf^-2*Af^-1*Bf*Af*Bf^-1*Af^-1,
Bf^-3*Af^-2*Bf^-3*Af^-2*Bf*Af*Bf^-1*Af^-1*Bf^-1*Af*Bf^-1*Af*Bf*Af^-1*Bf*Af,
Bf*Af*Bf*Af^-1*Bf^-1*Af^2*Bf*Af^2*Bf^-3*Af^-3*Bf^2*Af*Bf*Af*Bf^-3*Af^-1*Bf,
Bf^-1*Af*Bf^2*Af^-1*Bf^-1*Af*Bf^2*Af*Bf*Af^-1*Bf*Af^2*Bf^4*Af^-1*Bf^-1*Af^-1*Bf*Af*Bf^-1*Af^-1*Bf^-1*Af
];

# Finitely presented version of $\bar\Gamma$

GammaBarFP:=FreeGp/Rels;
A:=GammaBarFP.1;
B:=GammaBarFP.2;

# Important subgroup of $\bar\Gamma$

# These are Donald's
#index3aFP:=Group(A, B*A*B^-1, B^3);
#index3bFP:=Group(A, B^-2, (B*A^-1*B^-1)^2, B*A*B*A^-1*B^-1);

# These are Tim's
index3aFP:=Group(A, B*A*B^-1, B^3);
index3bFP:=Group(A, B^2, B*A^2*B^-1, B*A*B^-1*A^-1*B^-1);

index3aFCA:=FactorCosetAction(GammaBarFP,index3aFP);
index3bFCA:=FactorCosetAction(GammaBarFP,index3bFP);

# List of finite order elements

# These are Donald's
#T1:=A^-1*B^-1*A*B^-1;
#T2:=B*A*B^-1*B^-1*B^-1*A^-1;
#T3:=A*B*A^-1*B*A*B^-1*B^-1*B^-1*A^-1*A^-1*A^-1*B^-1;

# These are Tim's
T1:=B*A^-1*B*A;
T2:=A*B*B*B*A^-1*B^-1;
T3:=B*A*A*A*B*B*B*A^-1*B^-1*A*B^-1*A^-1;

FOList:=[T1,T2,T3];

# Check the indexes of the subgroup

Index(GammaBarFP,index3aFP) = 3;
Index(GammaBarFP,index3bFP) = 3;

# Check normality of the subgroup

IsNormal(GammaBarFP,index3aFP);
not IsNormal(GammaBarFP,index3bFP);

# Check that normalizers are as expected

Normalizer(GammaBarFP,index3aFP) = GammaBarFP;
Normalizer(GammaBarFP,index3bFP) = index3bFP;

# Calculate abelianizations of the subgroups

AbelianInvariants(GammaBarFP) = [2,2,3];
AbelianInvariants(index3aFP) = [2,2,7];
AbelianInvariants(index3bFP) = [2,2,2,9];

# Check which elements of finite order belong to which conjugates of
# which finite index subgroups 

List(FOList,k->k^index3aFCA);
List(FOList,k->k^index3bFCA);

# Check that the index 3 groups are torsion free

ForAll(FOList,k->MovedPoints(k^index3aFCA) = [1..3]);
ForAll(FOList,k->MovedPoints(k^index3bFCA) = [1..3]);

# Function to calculate fundamental group

FundGp:=function(G,FOList)
  local e1,e2,e3,e4,FCA,GFO,G0;
  e1:=GeneratorsOfGroup(GammaBarFP);
  e2:=Concatenation(e1,List(e1,Inverse));
  Add(e2,One(G));
  e3:=ListX(e2,e2,\*);
  FCA:=FactorCosetAction(GammaBarFP,G);
  GFO:=Concatenation(
    List(FOList,fo->
      List(Difference([1..Index(GammaBarFP,G)],MovedPoints(fo^FCA)),
        j -> fo^First(e3,e->j^(e^FCA) = 1)
      )
    )
  );
  Print("Size(GFO)=",Size(GFO),"\n");
  e1:=GeneratorsOfGroup(G);
  e2:=Concatenation(e1,List(e1,Inverse));
  Add(e2,One(G));
  e3:=ListX(e2,e2,\*);
  e4:=ListX(e2,e3,\*);
  G0:=Group(ListX(e4,GFO,
    function(elt,fo) return fo^elt; end
  ));
  Print(ForAll(GFO,fo->fo in G0),"\n");
  Print(IsNormal(G,G0),"\n");
  return FactorGroup(G,G0);
end;

StructureDescription(FundGp(GammaBarFP,FOList)) = "C2 x C2";

# This calculates the automorphism group of the corresponding surface
AutGp:=G->FactorGroup(Normalizer(GammaBarFP,G),G);

StructureDescription(AutGp(index3aFP)) = "C3";
StructureDescription(AutGp(index3bFP)) = "1";
