Minor bug fixed and small optimizations.