cleanup and refactored