Some abstraction and bugfixes.