Some refactoring for maintainability. Pull out the gradient checking code and the layers into separate files \ modules.