refactored for better error handling