Optimized some part of the code. For ticket #133.