Refactoring code and generalize