Better error messages and more idiomatic function defs