cleaned up a bunch of code duplication