Move and improve error function