Fixed avg pos computation, code clean-up