work in progress
refactored mean profile function