Merged with Omid's and Naveed's code.