Added code refactoring and documentation