Integrated Uij's code