Dynamic calculation of bg