added some exception handling and comments