EXHIBIT 10.5.1
                                                                                
                    SECOND AMENDMENT TO AMENDED AND RESTATED
                    ----------------------------------------
                       SERVICING SECURED CREDIT AGREEMENT
                       ----------------------------------

          THIS SECOND AMENDMENT TO AMENDED AND RESTATED SERVICING SECURED CREDIT
AGREEMENT (the "Amendment") is made and dated as of the 9th day of January,
1998, by and among THE FIRST NATIONAL BANK OF CHICAGO, a national banking
association ("FNBC"), BANK OF AMERICA NATIONAL TRUST AND SAVINGS ASSOCIATION, a
national banking association, THE BANK OF NEW YORK, a banking corporation
organized under the laws of the State of New York (all of the above individually
a "Lender" and, collectively, the "Lenders"), FNBC, as administrative agent for
the Lenders (in such capacity, the "Administrative Agent"), and HEADLANDS
MORTGAGE COMPANY, a California corporation (the "Company").

                                    RECITALS
                                    --------

          A.  Pursuant to that certain Amended and Restated Servicing Secured
Credit Agreement dated as of August 29, 1997 among the Administrative Agent, the
Lenders and the Company (as amended to date, the "Agreement"), the Lenders
agreed to extend credit to the Company on the terms and subject to the
conditions set forth therein.  All capitalized terms not otherwise defined
herein shall have the meanings given to such terms in the Agreement.

          B.  The Company filed a Form S-1 with the United States Securities
and Exchange Commission on October 20, 1997 (and subsequently filed an amended
Form S-1 on December 11, 1997) describing its plans to have an initial public
offering of its common stock (the "IPO"), copies of which Form S-1 and amended
Form S-1 have been distributed to the Lenders.

          C.  In connection with the IPO, the Company has requested certain
amendments and waivers to the Agreement and the Lenders have agreed thereto on
certain conditions, all as more particularly described below.
 
          NOW, THEREFORE, in consideration of the foregoing Recitals and for
other good and valuable consideration, the receipt and adequacy of which are
hereby acknowledged, the parties hereto hereby agree as follows:

                                   AGREEMENT
                                   ---------

          1.   Payment of Dividends.
               -------------------- 

               (a) Upon the occurrence of the IPO, the Company will no longer be
an S Corporation under the Code. The Company desires to make a distribution to
its current shareholders, in an aggregate amount not to exceed the amount of the
Company's aggregate taxable income which has not been previously distributed
(the "Shareholder Distribution"), from a portion of the proceeds from the IPO.
The Lenders hereby approve the Shareholder 

                                       1

 
Distribution, such approval to be effective as of the IPO Effective Date (as
defined in Paragraph 11(b) below) if and when it occurs.

               (b) Effective as of the IPO Effective Date, Paragraph 8(f) of the
Agreement is hereby amended to read in its entirety as follows:
 
                     "8(f) Payment of Dividends.  (1) Declare or pay any 
                           --------------------             
     dividends upon any shares of the Company's stock now or hereafter
     outstanding, except dividends payable in the capital stock of the Company,
     or (2) make any distribution of assets to its stockholders as such, whether
     in cash, property or securities, or (3) set aside any of its property for
     the purpose of doing any of the foregoing; provided, however, that the
     Company may pay, from time to time during any fiscal year of the Company,
     dividends and distributions in an aggregate amount not to exceed twenty-
     five percent (25%) of the amount equal to the Adjusted Net Income of the
     Company to date for such fiscal year (calculated as of each fiscal quarter
     end for such fiscal year); and provided further, that no dividend payment
     otherwise permitted above shall be made if, immediately before or after
     giving effect thereto, any Event of Default or Potential Default exists or
     would exist."

               (c) Effective as of the IPO Effective Date, a new definition of
the term "Adjusted Net Income" is hereby added to Paragraph 12 of the Agreement
in correct alphabetical order to replace the definition of the term "Pre-Tax
Income," to read in its entirety as follows:

              "'Adjusted Net Income' shall mean for any period the positive
                 -------------------                                        
     consolidated net income of the Company for such period, determined in
     accordance with the methodology described on Schedule II."
                                                  -----------  

               (d) Effective as of the IPO Effective Date, Schedule II to the
                                                           -----------       
Agreement is hereby replaced by Replacement Schedule II attached hereto.
                                -----------------------                 

           2.  Prepayment of Subordinated Debt.
               ------------------------------- 
 
               (a) The Company desires to prepay in full all outstanding
principal and accrued interest (in an aggregate amount not exceeding
$11,000,000.00) in connection with certain unsecured Subordinated Debt of the
Company to Peter Paul and/or Jessica Paul (the "Paul Subordinated Debt") with a
portion of the proceeds from the IPO. The Lenders hereby approve such prepayment
of the Paul Subordinated Debt, such approval to be effective as of the IPO
Effective Date if and when it occurs.

               (b) Effective as of the IPO Effective Date, Exhibit K to the 
                                                           ---------
Agreement is hereby replaced by Replacement Exhibit K attached hereto.
                                ---------------------                 
 
          3.  Purchase or Retirement of Stock.  The Company desires the
              -------------------------------                          
flexibility to repurchase and retire shares of its common stock once it becomes
a public company and the Lenders have agreed to provide such flexibility as long
as no Event of Default or Potential 

                                       2

 
Default exists. Effective as of the IPO Effective Date, Paragraph 8(g) of the
Agreement is hereby amended by adding the phrase "if, immediately before or
after giving effect to any such acquisition, purchase, redemption or retirement,
any Event of Default or Potential Default exists or would exist" after the word
"outstanding" at the end of the paragraph.
 
          4.  Minimum Net Worth.
              ----------------- 
 
              (a) To reflect the agreement of the parties hereto to change the
minimum net worth requirements under the Agreement, effective as of the
Effective Date (as defined in Paragraph 11(a) below), Paragraph 8(k) of the
Agreement is hereby amended to read in its entirety as follows:
 
                   "8(k) Minimum Net Worth.  Permit at any date its
                         -----------------                             
     consolidated:

                         (1) Effective Net Worth to be less than the greater of:
          (i) the amount equal to $40,000,000.00 plus fifty percent (50%) of the
          Company's consolidated annual net income (if positive) earned each
          calendar year commencing with the year 1998, and (ii) the amount equal
          to eighty percent (80%) of its consolidated Effective Net Worth as of
          the end of the month of occurrence of the initial public offering of
          the Company's common stock, minus such additional reductions
          thereafter that may occur as a result of the final determination of
          the exact amount of the Shareholder Distribution, plus fifty percent
          (50%) of the Company's consolidated annual net income (if positive)
          earned each calendar year (with the first year being a partial year
          commencing the month immediately following the month of occurrence of
          the initial public offering of the Company's common stock); or
     
                         (2) Adjusted Tangible Net Worth to be less than the
          greater of: (i) the amount equal to $70,000,000.00 plus fifty percent
          (50%) of the Company's consolidated annual net income (if positive)
          earned each calendar year commencing with the year 1998, and (ii) the
          amount equal to eighty percent (80%) of its consolidated Adjusted
          Tangible Net Worth as of the end of the month of occurrence of the
          initial public offering of the Company's common stock, minus such
          additional reductions thereafter that may occur as a result of the
          final determination of the exact amount of the Shareholder
          Distribution, plus fifty percent (50%) of the Company's consolidated
          annual net income (if positive) earned each calendar year (with the
          first year being a partial year commencing the month immediately
          following the month of occurrence of the initial public offering of
          the Company's common stock)."
          
          (b) Effective as of the Effective Date, a new definition of the term
"Shareholder Distribution" is hereby added to Paragraph 12 of the Agreement in
correct alphabetical order to read in its entirety as follows:

                                       3

 
                "'Shareholder Distribution' shall mean the distribution by the
                 ------------------------                                    
     Company to its shareholders, in an aggregate amount not to exceed the
     amount of the Company's aggregate taxable income which has not been
     previously distributed, from a portion of the proceeds from the initial
     public offering of the Company's common stock, which distribution has been
     expressly approved in writing by one hundred percent (100%) of the
     Lenders."
 
          5.  Current Ratio.  To reflect the agreement of the parties hereto to
              -------------                                                    
exclude certain assets of the Company in the determination of its Current Ratio,
effective as of the Effective Date, the definition of the term "Current Ratio"
set forth in Paragraph 12 of the Agreement is hereby amended to read in its
entirety as follows:
 
               "'Current Ratio' shall mean at any date the ratio of current
                 -------------                                             
     assets to current liabilities of the Company and its Subsidiaries,
     determined on a consolidated basis in accordance with GAAP; provided,
     however, that in determining the Company's current assets for purposes
     hereof, the following assets shall be excluded: (a) the aggregate
     outstanding principal amount of all loans and advances outstanding to
     officers and employees of the Company and its Subsidiaries, and (b) any
     transferor interests retained by the Company in any securitizations of
     HELOCs and interest only strips receivables that will not be realized in
     the immediately following twelve-month period."

          6.  Stock Ownership.  To reflect the agreement of the parties hereto
              ---------------                                                 
to change the stock ownership requirement in anticipation of the IPO, effective
as of the IPO Effective Date, Paragraph 9(i) of the Agreement is hereby amended
to read in its entirety as follows:
 
               "9(i)  Peter Paul shall cease to own at least twenty-five
     percent (25%) of the outstanding shares of capital stock of the Company; or
     any other Person, or Persons acting in concert, shall hold, directly or
     indirectly, beneficial interests in more shares of capital stock than Peter
     Paul does."

          7.  Reduction in Pricing.  To reflect the agreement of the Lenders to
              --------------------                                             
reduce the interest rates applicable to certain Loans upon the occurrence of the
IPO, effective as of the IPO Effective Date, the definitions of the terms
"Eurodollar Spread" and "Federal Funds Pricing Spread" set forth in Paragraph 12
of the Agreement are hereby amended to read in their entirety as follows:
 
               "'Eurodollar Spread' shall mean one and one-quarter of one
                 -----------------                                       
     percent (1.25%).

               'Federal Funds Pricing Spread' shall mean one and three-eighths
                -----------------------------                                 
     of one percent (1.375%). "

          8.  Reduction in Facility Fee.  To reflect the agreement of the
              -------------------------                                  
Lenders to reduce the commitment fee payable by the Company upon the occurrence
of the IPO, effective as of the IPO Effective Date, subparagraph 3(l)(1) of the
Agreement is hereby amended to replace the phrase 

                                       4

 
"seven-twentieths of one percent (0.35%) per annum" set forth therein with the
phrase "one-quarter of one percent (0.25%) per annum."
 
          9.  Reaffirmation of Other Loan Documents.  The Company hereby affirms
              -------------------------------------                             
and agrees that (a) the execution and delivery by the Company of and the
performance of its obligations under this Amendment shall not in any way amend,
impair, invalidate or otherwise affect any of the obligations of the Company or
the rights of the Secured Parties under the Security Agreement or any other Loan
Document, (b) the term "Obligations" as defined in Paragraph 12 of the Agreement
includes, without limitation, the Obligations of the Company under the Agreement
as amended by this Amendment, (c) the Security Agreement remains in full force
and effect and such agreement constitutes a continuing first priority security
interest in and lien upon the Collateral, and (d) for any and all purposes, any
reference to the Agreement following the effective date of this Amendment shall
constitute a reference to the Agreement as amended to date, including, without
limitation, by this Amendment.

          10.  Modification of Related Documents.  All reports and other forms
               ---------------------------------                              
utilized in connection with the day-to-day operations of the credit facility
evidenced by the Agreement shall be deemed modified consistent with the
provisions of this Amendment.

          11.  Effective Date and IPO Effective Date.
               ------------------------------------- 

               (a) The amendments set forth in Paragraphs 4 and 5 above shall be
effective on the earliest date (the "Effective Date") upon which the
Administrative Agent has received (a) duly executed copies of this Amendment
from each of the Lenders, the Administrative Agent and the Company, and (b) such
board resolutions, incumbency certificates and other additional documentation as
the Administrative Agent may request in connection herewith.

               (b) The amendments set forth in Paragraphs 1, 2, 3, 6, 7 and 8
above shall be effective on the earliest date (the "IPO Effective Date") after
the Effective Date upon which the IPO shall occur and each of the following
statements shall be true: (i) the IPO involves the offering of between 4,500,000
and 5,700,000 shares of capital stock of the Company to the public market, (ii)
there is no violation of any rule, regulation or requirement under the
Securities Act of 1933, as amended, or the Securities Exchange Act of 1934, as
amended, in connection with the IPO, and (iii) the Net Proceeds from the IPO is
not less than $20,000,000.00. For the purposes hereof, "Net Proceeds" shall mean
the gross proceeds from the IPO minus all underwriter fees and expenses, the
aggregate amount of the Shareholder Distribution, and the aggregate amount
required to repay in full all outstanding amounts under the Paul Subordinated
Debt.

          12.  Representations and Warranties.  The Company hereby represents
               ------------------------------                                
and warrants to the Administrative Agent and the Lenders as follows:

               (a) The Company has the corporate power and authority and the
legal right to execute, deliver and perform this Amendment and has taken all
necessary corporate action to authorize the execution, delivery and performance
of this Amendment. This Amendment has been duly executed and delivered on behalf
of the Company and constitutes the legal, valid and 

                                       5

 
binding obligation of the Company enforceable against the Company in accordance
with its terms. The execution, delivery and performance of this Amendment will
not violate any Requirement of Law or Contractual Obligation or require any
consent, approval or authorization of, or registration, declaration or filing
with, any Governmental Authority.

               (b) At and as of the date of execution hereof and at and as of
the effective date of this Amendment and both prior to and after giving effect
hereto: (1) the representations and warranties of the Company contained in the
Loan Documents are accurate and complete in all respects, and (2) there has not
occurred an Event of Default or Potential Default.

          13.  No Other Amendment.  Except as expressly amended herein, the Loan
               ------------------                                               
Documents shall remain in full force and effect as currently written.

          14.  Counterparts.  This Amendment may be executed in any number of
               ------------                                                  
counterparts, each of which when so executed shall be deemed to be an original
and all of which when taken together shall constitute one and the same
agreement.

                                       6

 
          IN WITNESS WHEREOF, the parties hereto have caused this Amendment to
be executed as of the day and year first above written.

                HEADLANDS MORTGAGE COMPANY,
                a California corporation

                By:
                    ------------------------------    

                Name:
                     ----------------------------- 
 
                Title:
                      ----------------------------
 
                THE FIRST NATIONAL BANK OF CHICAGO,
                a national banking association, as 
                Administrative Agent and a Lender

                By:
                    ------------------------------    

                Name:
                     -----------------------------

                Title:
                      -----------------------------

                BANK OF AMERICA NATIONAL TRUST 
                AND SAVINGS ASSOCIATION,
                a national banking association, 
                as a Lender

                By:
                    ------------------------------    

                Name:
                     -----------------------------

                Title:
                      ----------------------------

                THE BANK OF NEW YORK,
                a banking corporation organized under
                the laws of the State of New York, 
                as a Lender

                By:
                    ------------------------------    

                Name:
                     -----------------------------

                Title:
                      -----------------------------

                                       7

 
                                                         REPLACEMENT SCHEDULE II
                                                         -----------------------


                                  SCHEDULE II:
               METHODOLOGY FOR COMPUTATION OF ADJUSTED NET INCOME


 
 
Net Income                                             $XX
                                                 
                   LESS
            
FAS 122       Gain On Sale of Mortgages                 XX
            
FAS 122       Decrease in Deferred Tax Liability        XX
            
                  PLUS
            
FAS 122       Loss On Sale of Mortgages                 XX
            
FAS 122       Service Release Premiums                  XX
            
FAS 122       Amortization and Impairment
                  of Servicing Rights                   XX
            
FAS 122       Net Book Value of Servicing
                  Assets Sold                           XX
            
FAS 122       Increase in Deferred Tax Liability        XX
                                                       ----

ADJUSTED NET INCOME                                  $  XX


                                       1

 
                                                           REPLACEMENT EXHIBIT K
                                                           ---------------------


                                  SCHEDULE OF
                              PERMITTED OTHER DEBT
                                      AND
                        (*) PERMITTED OTHER SECURED DEBT


1.   Indebtedness owed under repurchase agreements and gestation repurchase
     credit facilities entered into by the Company from time to time with
     financial institutions approved by the Administrative Agent and the
     Majority Lenders in an aggregate amount not to exceed at any one time
     outstanding $450,000,000.00.*

2.   Indebtedness owed under any servicing secured facility (including the
     Agreement) in an aggregate amount not to exceed at any one time outstanding
     $30,000,000.00.*

3.   Indebtedness owed under credit facilities entered into by and between the
     Company and Residential Funding Corporation ("RFC") from time to time
     secured by Mortgage Loans that are delinquent or in foreclosure or subject
     to a Take-Out Commitment issued by RFC, manufactured housing loans and REO
     properties in an aggregate amount not to exceed at any one time outstanding
     $15,000,000.00.*

4.   Indebtedness owed under any deposit-backed interest rate exchange
     agreements and/or investment arbitrage lines, entered into in the ordinary
     course of business.*

5.   Indebtedness of HMSI to third party lenders in an amount not to exceed
     $5,000,000.00 in the aggregate at any time outstanding, the proceeds of
     which Indebtedness shall be used by HMSI to finance advance receivables.*

6.   Indebtedness of HMSI secured by liens on the retained interests in
     securitizations of HMSI in connection with yield maintenance arrangements
     on securities issued through HMSI.


                                       1