Refactored a bit