final code for first phase