Revised code, works up to section 3
revision works up to section 4