Some small changes to @koencertyn's code