Need help, code works fine but the final condition doesn't satisfy. is it a bug?