Refactored calendar code to remove duplication