Finalized refactoring. Needs to be reviewed by teammate before approval.