Mostly fix this bug. Still some issues left. #329