added support for pdf, tried to fix a few bugs