Integrated Daniel's code