added error handling and improved parameter validation