UPD: improved error handling