generalized code

please review.