A bunch of refactoring. I know I probably missed a few things and admin.js needs more comments, but it's a start.