edit code for better, more general