Moved all the local brightness calculation code to get_local_brightness()
Fixed a reference to freed memory and a logical error in get_local_brightness()