Code restructuring. Added some comments.