change how undefined is checked
changed error handling, still unsatisfactory