refactoring some of the code