Documentation of the code added. Minor bug fixing and code refactorig.