Added better error checking and refactored getHistory.