original code by trg787 added
updated license to match original