Reorganized profiling code to DRY style