copied most of the portions in