Refactoring and documentation added