Most logic converted to ruby