Bring general sketch commits upstream

Move the general parts of PR #1178 upstream decrease the maintenance work.
Does it make sense to put the implementation into a compile unit?