code duplication done, some minor left