Restructure code and add types and docs.