added extra checks at intermediate stages, added intermediate stage for sort just in case that fails