getDocument - code reorganized and optimized