Improved gain factor calculation
Added pi result back to output