rebased #63 with small fixes