refactored with comments