latest code with both parts working