small optimizations
fixed invalid document access