fixes #2411 Refactor /lib directory

to app/models/concerns or app/controllers/concerns and app/services, plus other code cleanups