Partly ported to libLolien