pipelined version
move to more modular design