Made code tolerable to 3.x (hopefully without new 2.x bugs)