now with dynamic document type and a cleaner implementation