refactored code to be more robust and clean