More code improvements and refactoring