added parseDocument method and rewritten complete logic