reorganised code refs #1