Restructured code to be easier to understand.