First rev, buggy
Updated version, untested