Const correctness

This introduces some const correctness to various files, as requested by #842. This is only a beginning, and more can be done, but I thought I'd submit what I have anyway, because it's a lot of work, and it would have been in vain if it gets rejected...