Some comments added and code refactoring.