refactored code to allow pipelines
First working version