Integrated Josh's code