Massive (working) code cleanup