working version, almost done