partially rewritten