added updated code based on misko's comments