better error messages, fucntion encapsulation