Initial working version; probably ugly