Ported to 1.2.2 and massive changes