Basic fixes of dirtiest code I ever seen