Full rewrite by Julia -- submitted version