Huge refactor to support different document types