Added history functions, cleaned up code.