slight improvements and refactoring