Copied from plankton-toolbox under MIT licensed code by Arnold Andreasson (mellifica.se)