Updated code with listing functions and error handling