Updated final code