refactored a bunch of redundant code