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

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

# Finitely presented version of $\bar\Gamma$

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


# Important subgroup of $\bar\Gamma$

# This is Donald's
#index3FP:=Group(A,C,B*A*B^-1,B^3);

# This is Tim's
index3FP:=Group(A, C, B*A*B^-1, B^3);

index3FCA:=FactorCosetAction(GammaBarFP,index3FP);

# List of finite order elements

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

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

FOList:=[T1,T2,T3];

# Check the index of the subgroup

Index(GammaBarFP,index3FP) = 3;

# Check normality of the subgroup

IsNormal(GammaBarFP,index3FP);

# Calculate abelianizations of the subgroups

AbelianInvariants(GammaBarFP) = [3];
AbelianInvariants(index3FP) = [7];

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

List(FOList,k->k^index3FCA);

# Check that the index 3 group is torsion free

ForAll(FOList,k->MovedPoints(k^index3FCA) = [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)) = "1";

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

StructureDescription(AutGp(index3FP)) = "C3";
