refactored for better handling