Rearranged and added comments in logical steps