stop profile switch from crashing (issue #517); also make print_profile use a loop instead of recursion