A working version... needs some functional testing...