Apenas melhorei o código no geral. Testado e a funcionar sem erros!