Refactoring of survey script by Andy Boughton