improve the code of assignment 2