Improved code. Added comments.