Port from ruby code