Refactored the code a bit.