ported most code