Merge 1st working prototype patch from original implementation