Added start of much better error handling