Refactored a bit. We really need to add decorators for all this checking.