beautified and refactored