Final Working code