merged Jonathans code