Some refactoring and elimination of repetitive codes