redid the whole long and properly... borrowed from flask examples.