Comments added. Code refactorized.