updated and refactored