Pasted @Gen2ly's implementation over top of original.