Testing linearity and modelling nonlinear time series