refactored and sped up some more