refactored a bit