refactoring redundant code.

also doing some error searching