Merge Jakob's magic code