Major reorganization of functions

Split the "spatial.R" functions into four scripts