Updated code to add a scale stage in a pipepline
Added calculate difference stage to the pipeline
Removed unused function
Rearranged code order, added some comments and moved some channel closes to the correct place to avoid race issues