refactioned code and comments