refactor modified code from original ones in lightmaps.py