Various improvements to the valgrind and profiling scripts