slight generalization of code