First version almost complete - needs testing