Refactoring and cleanup
Quickfixes on memory handling