Refactored common code. Ensured cond var  notification in case of failures