First complete version. Not tested