Refactored and cleaned up to make it suitable for the Yesod Cookbook