reduce code duplication. working on bug