first merge of all the code bits, completely untested yet