updated with new features and added error handling.