mshah - more refactoring to make it readable