refactored
added some comments