Added some comments. Slightly refactored code.