extract more functions and fix --profile.out handling bug