Performed some refactoring and added more documentation.