Modified the function, should hopefully be more accurate now.