Output is correct but code is not working