Port some stuff from JB