Some fixes for @koencertyn's code