Add error/misconfiguration checks and fix undefined variables