Generalized the code

better code