final code improved