code improvement and comments added