#39: Code refactoring and bug fixing.