you have a lot code duplication. Replace it by single function.