This has gone quite long -- please extract a func