refactoring

moving common functionality to functions