Add tests, adjust some minor tweaks, and used functions that are compatible with tornado 3.2