Improved code, added some comment. No functional changes.