Refactored to support flexible hidden layers