Finish the code & tests of phase3