refactored reply handling