Added state stack, simplified main