refactoring the code and adding missing methods