Added exception handling and documentation