initial import from hansz fork