Added some exception handling and tidied up the naming.