Refactor profiling code