Reorganized code. Added comments.