added some checks to make sure the 1/weight doesnt produce an error.