refactor and added comments
more comments