small refactor and added some comments