Updated plugin with better document handling