error handling and improved reusability