This file is a mess in general. A rewrite is probably in order.