Final updated code