Refactoring and added documentation comments