Several performance enhancements that should be done here. I can rework this before it's merged.