updated final code