Based off JBC sources and Bigsupersquid's tweaks