Code cleanup, bugfixes.