generalize and clean code