Second try of solving prog assignment 2