Reworked based off Oliver MacNeely's version.