feature/refactor document form

This simplifies the document for processing a lot. There are still some details to iron out, but overall this code is much easier to understand, and less error prone.