removed a lot of duplicate code