Ported most of the functionality