merge Gav's refactoring and issue/#1 into MASTER