first correct, albeit dirty, version