working code minus closing functionality