more bugfixes courtesy of clintxs