moved all function in more or less correct places