commmented, refactored & formatted