Finally \AtBeginDocument fixed