Incorporated some code from experimental branch