implemented machine and fixed some bugs