Refactored to do it properly