Added exception handling and doctrings