refactored some code
added mass of comments