const-correctness and some minor performance improvements.

Added const-correctness where I could and so far as I know, the application's behavior has not been changed. Performance improvements come from passing certain parameters by reference (and not by value).