started some refactoring to remove duplicate code