Removed all excess repeated code and made more functions to handle it so it is DRY.