Cleanup day #4

More code cleanup, major bugfix.