Refactor and bug-fix StackMachine.java