refactoring + added some comments