minor generalizations and restructuring