Refactored a bit for my own sanity.