Improve f() and g(), fix for mac