rewritten all in ruby; crossplatf