lots of changes, not perfect, needs tidied and cleaned