almost working but havn't clean up the code yet