refactor and improve error message