Recent version from Maged merged with Ahmed's code on 17 Sept