trying to DRY up the code and optimize it