changes to most of the code - not tested