Revised code to pull into lib and clean up contoller. Also revised lib into modules.