big refactor: now we can save document \o/