works now, but needs more tests