Implemented all functions. Not exhaustively tested.