First rewrite, but still missing many features