At last the code works