Refactored to remove a bunch of duplicate code plus cleaned it up
Refactored to remove more duplicate code