Refactored a bit and made things a little neater