Fix LaTeX injection. Thanks, @anoek
Cleaning up code quite a bit