fork of the relevant parts from HsUtils