refactor, added some comments