#117 (Could not import patch but inspired from it plus some new additions) Import patch from github