Code cleanup, generalization of animation state code, a few small bugfixes.