Restoring and checking PR138 code
rebased