need to refactor this redundant logic more. This is a start