clean up private internals
rename document -> doc to avoid confusion with DOM