This is a squash of two commits:

- Small code refactoring
- Fix major memory leak