Full code with the modifications