Refactored the code slightly.