Added comments and smaller refactoring.