Multiple changes to make it work smoothly and produce the desired pdf output. References still need some work.