Refactor code and improve warning logic.