Refactored a bit based on review comments