improved logic to for preparing the update
Improved error handling and tests