Re-arranged code based on Aarons comments