Refactor and generalize