Large fixes, needs testing.