refactored, need to check when exactly to calculate f, g, h vals