some fixes courtesy of Phil3789