added desired features and recent advice from ZR
reinstated main code