Code refactoring. Minor bug fixes.