Improved error handling for metadata processing. Fixed shift bug